package com.cy.pj.goods.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Cx
 * @stage secondStageOfWeb
 * @creat 2020-11-02 20:32
 */
@Repository
public class DefaultGoodsDao implements GoodsDao {

    @Autowired
    private DataSource dataSource;


    @Override
    public List<Map<String, Object>> findGoods() throws Exception {
        Connection conn = dataSource.getConnection();
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery("select * from tb_goods");
        List<Map<String, Object>> list = new ArrayList<>();
        ResultSetMetaData rsmd = rs.getMetaData();
        while (rs.next()) {
            HashMap<String, Object> map = new HashMap<>();
            for (int i = 1; i <=rsmd.getColumnCount() ; i++) {
                map.put(rsmd.getColumnLabel(i), rs.getObject(i));
            }
            list.add(map);
        }
        rs.close();
        stat.close();
        conn.close();
        return list;
    }

}
